package cn.sccl.mybatis.junt;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.sccl.mybatis.mapper.UserMapper;
import cn.sccl.mybatis.pojo.QueryVo;
import cn.sccl.mybatis.pojo.User;

/**
 * @author 饶爽
 * @time 2019年9月21日 下午5:04:59
 * @version 1.0.0
 * User的mapper动态代理测试
*/
public class MybatisMapperTest {
    public SqlSessionFactory sqlSessionFactory;
    @Before
    public void before() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
    }
    @Test
    public void testMapperFindUserById() throws Exception {
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //SqlSEssion帮我生成一个实现类  （给接口）
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        
        User user = userMapper.findUserById(10);
        System.out.println(user);
        sqlSession.close();
    }
    @Test
    public void testQueryUserByUsername() throws Exception {
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //SqlSEssion帮我生成一个实现类  （给接口）
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        
        List<User> list = userMapper.queryUserByUsername("张");
        for (User user : list) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    @Test
    public void testInsertUser() throws Exception {
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //SqlSEssion帮我生成一个实现类  （给接口）
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 创建保存对象
        User user = new User();
        user.setUsername("刘小备");
        user.setBirthday(new Date());
        
        userMapper.insertUser(user);
        System.out.println(user);
        // 和spring整合后由spring管理
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void testMapper4() throws Exception {
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //SqlSEssion帮我生成一个实现类  （给接口）
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 创建保存对象
        QueryVo vo = new QueryVo();
        User user = new User();
        user.setUsername("小");
        vo.setUser(user);
        
        List<User> list = userMapper.findUserByQueryVo(vo);
        for (User user2 : list) {
            System.out.println(user2);  
        }
        // 和spring整合后由spring管理
        sqlSession.close();
    }
    @Test
    public void testQueryUserCount() throws Exception{
        // mybatis和spring整合，整合之后，交给spring管理
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // 创建Mapper接口的动态代理对象，整合之后，交给spring管理
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 使用userMapper执行查询用户数据条数
        int count = userMapper.queryUserCount();
        System.out.println(count);

        // mybatis和spring整合，整合之后，交给spring管理
        sqlSession.close();
    }
    //  根据性别和名字查询用户
    @Test
    public void testfindUserBySexAndUsername() throws Exception {
        //创建SqlSession
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        //user.setSex("1");
        user.setUsername("张小明");
        List<User> users = userMapper.selectUserBySexAndUsername(user);
        for (User user2 : users) {
            System.out.println(user2);
        }
    }
//  根据多个id查询用户信息  
    @Test
    public void testSelectUserByIds() throws Exception {
        //创建SqlSession
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<Integer> ids  = new ArrayList<>();
        ids.add(16);
        ids.add(22);
        ids.add(24);
        
        QueryVo vo = new QueryVo();
        vo.setIdsList(ids);
        List<User> users = userMapper.selectUserByIds(vo);
        for (User user2 : users) {
            System.out.println(user2);
        }
    }
    @Test
    public void testSelectUserByIds2() throws Exception {
        //创建SqlSession
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Integer[] ids = new Integer[3];
        ids[0]=16;
        ids[2]=24;
        ids[1]=22;
        List<User> users = userMapper.selectUserByIds2(ids);
        for (User user2 : users) {
            System.out.println(user2);
        }
    }
    @Test
    public void testSelectUserByIds3() throws Exception {
        //创建SqlSession
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(16);
        ids.add(22);
        ids.add(24);
        List<User> users = userMapper.selectUserByIds3(ids);
        for (User user2 : users) {
            System.out.println(user2);
        }
    }
}
